﻿package com.wandian.model
{
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.http.UserData;
	import com.wandian.model.socket.ServerPacketProxy;
	import com.wandian.model.socket.net.Command;
	import com.wandian.model.vo.*;
	
	import org.puremvc.as3.patterns.proxy.Proxy;

	/**
	* @author long
	* @QQ 108232706
	* @version 1.0
	*/
	public class UserInfoProxy extends Proxy
	{
		private var _userVo:UserVo;
		/**
		 * 牌友列表
		 * @default
		 */
		private var _arrFriend:Array;
		private var _global:GameData;
		private var _serverPoxy:ServerPacketProxy;
		public static const NAME:String="UserInfoProxy";

		public function UserInfoProxy(param1:Object=null)
		{
			this._userVo=new UserVo();
			this._arrFriend=[];
			this._global=GameData.getInstance();
			super(NAME, param1);
			return;
		}

		override public function onRegister():void
		{
			trace("UserInfoProxy onRegister");
			this._serverPoxy=facade.retrieveProxy(ServerPacketProxy.NAME) as ServerPacketProxy;
		}

		public function get userVo():UserVo
		{
			return this._userVo;
		}

		public function get arrFriend():Array
		{
			return this._arrFriend;
		}

		public function setUserVo(param1:Object):void
		{
			var _loc_2:String=null;
			for (_loc_2 in param1)
			{

				this._userVo[_loc_2]=param1[_loc_2];
			}
			return;
		}

		public function set money(money:Number):void
		{
			trace("更新了练习币：" + money);
			this._userVo.money=money;
			this.sendNotification(CLZNoticeName.UPDATE_MONEY, this.userVo.money);
			return;
		}

		public function set gold(param1:uint):void
		{
			trace("更新了金币：" + param1);
			this._userVo.gold=param1;
			this.sendNotification(CLZNoticeName.UPDATE_GOLD, this.userVo.gold);
			return;
		}

		public function set bankAssets(param1:uint):void
		{
			trace("更新银行存款：" + param1);
			this._userVo.bankAssets=param1;
			this.sendNotification(CLZNoticeName.UPDATE_BANKASSETS, this.userVo.bankAssets);
			return;
		}

		public function set level(_level:uint):void
		{
			this._userVo.level=_level;
			this.sendNotification(CLZNoticeName.UPDATE_LEVEL, this._userVo.level);
			return;
		}

		public function set rank(param1:uint):void
		{
			if (this._userVo.rank == 0)
			{
				this._userVo.rank=param1;
				this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.replaceStrValue(GameData.tip["MY_TIP_RANK_NUM"], "NUM", param1)});
				this.sendNotification(CLZNoticeName.UPDATE_RANK_USER, this._userVo);
				return;
			}
			if (param1 != this._userVo.rank)
			{
				if (param1 < this._userVo.rank)
				{
					this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.replaceStrValue(GameData.tip["TIP_RANK_ADD"], "NUM", param1)});
				}
				else
				{
					this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.replaceStrValue(GameData.tip["TIP_RANK_SUB"], "NUM", param1)});
				}
			}
			this._userVo.rank=param1;
			this.sendNotification(CLZNoticeName.UPDATE_RANK_USER, this._userVo);
			return;
		}

		public function getUserInfoById():void
		{
			var _loc_1:*=new UserData();
			_loc_1.getUserInfo(this.responsetUserInfoById, this.responseUserInfoErr);
			return;
		}

		public function responsetUserInfoById(param1:Object):void
		{
			var _loc_2:Object=null;
			if (param1.svflag != 1)
			{
				_loc_2={okNoticeName: CLZNoticeName.RELOAD_WINDOW_PAGE, tip: GameData.tip["GET_USER_DATA_ERR"], tipTitle: GameData.tip["SYSTEM_TIP"], isCancel: false};
				this.sendNotification(CLZNoticeName.SHOW_WINDOWS_TIP, _loc_2);
				return;
			}
			this.setUserVo(param1.data.aUser);
			this.setUserVo(param1.data.aGame);
			this._userVo.canaward=param1.data.canaward;
			this._userVo.lotcount=param1.data.lotcount;
			this._userVo.isopen=param1.data.isopen;
			this._userVo.isdayfrist=param1.data.isdayfrist;
			this._userVo.tcost=param1.data.tcost;
			this._userVo.isaddf=param1.data.isaddf;
			this._userVo.isInviteopen=param1.data.isInviteopen;
			this.sendNotification(CLZNoticeName.GET_USER_INFO_BYID_SUCC, param1);
			return;
		}

		private function responseUserInfoErr(param1):void
		{
			if (param1.info.code == "NetConnection.Connect.Success")
			{
				return;
			}
			this.sendNotification(CLZNoticeName.GET_USER_INFO_BYID_ERR);
			return;
		}

		/**
		 * 请求好友列表
		 */
		public function requestFriendList():void
		{
			this._serverPoxy.writeBegin(Command.CMD_FriendList);
//			this._serverPoxy.writeByte(roomid);
			this._serverPoxy.writeEnd();
			this._serverPoxy.sendCmd();
			return;
		}

		/**
		 * 服务器响应好友列表信息
		 * @param param1
		 */
		public function responseFriendList():void
		{
			this._arrFriend=[];
			var friendVo:FriendVo=null;
			var total:int=this._serverPoxy.readInt();
			var arrRoomPlayers:Array=[];
			var count:int=0;
			while (count < total)
			{

				friendVo=new FriendVo();
				friendVo.userid=this._serverPoxy.readInt();
				friendVo.icon=this._serverPoxy.readString();
				friendVo.name=this._serverPoxy.readString();
				friendVo.headImage=this._serverPoxy.readBytes();
				friendVo.sex=this._serverPoxy.readByte();
//				friendVo.gold=this._serverPoxy.readInt();
				friendVo.money=this._serverPoxy.readFloat();
				friendVo.level=GameData.getLevel(friendVo.money);
				friendVo.state=this._serverPoxy.readByte();
				friendVo.curRoomId=this._serverPoxy.readInt();
//				friendVo.state=this._serverPoxy.readByte();
//				friendVo.rank=this._serverPoxy.readInt();
//				friendVo.level=GameData.getLevel(friendVo.gold);
				_arrFriend.push(friendVo);
				count=count + 1;
			}
			this.sendNotification(CLZNoticeName.FRIEND_LIST_RES, this._arrFriend);
			return;
		}

		public function addFriend(param1:FriendVo):void
		{
			this._arrFriend.push(param1);
			this.sendNotification(CLZNoticeName.UPDATE_FRIEND_STATE, this._arrFriend);
			return;
		}

		public function removeFriend(param1:int):void
		{
			var _loc_4:FriendVo=null;
			var _loc_2:*=this._arrFriend.length;
			var _loc_3:int=0;
			while (_loc_3 < _loc_2)
			{

				_loc_4=this._arrFriend[_loc_3] as FriendVo;
				if (_loc_4.userid == param1)
				{
					this._arrFriend.splice(_loc_3, 1);
					break;
				}
				_loc_3=_loc_3 + 1;
			}
			this.sendNotification(CLZNoticeName.UPDATE_FRIEND_STATE, this._arrFriend);
			return;
		}

		public function updateFriendState(param1:uint, param2:int):void
		{
			var _loc_5:FriendVo=null;
			var _loc_3:*=this._arrFriend.length;
			var _loc_4:int=0;
			while (_loc_4 < _loc_3)
			{

				_loc_5=this._arrFriend[_loc_4] as FriendVo;
				if (_loc_5 != null)
				{
				}
				if (_loc_5.userid == param1)
				{
					_loc_5.state=param2;
					if (param2)
					{
						this._arrFriend.unshift(_loc_5);
						this.sendNotification(CLZNoticeName.ONLIN_FRIEND, _loc_5);
					}
					else
					{
						this._arrFriend.push(_loc_5);
						this.sendNotification(CLZNoticeName.OFF_FRIEND, _loc_5);
					}
					break;
				}
				_loc_4=_loc_4 + 1;
			}
			this.sendNotification(CLZNoticeName.UPDATE_FRIEND_STATE, this._arrFriend);
			return;
		}

		public function updateMoreFriendState(param1:Array, param2:int=1):void
		{
			var _loc_5:FriendVo=null;
			var _loc_6:int=0;
			var _loc_3:*=this._arrFriend.length;
			var _loc_4:int=0;
			while (_loc_4 < _loc_3)
			{

				_loc_5=this._arrFriend[_loc_4] as FriendVo;
				_loc_6=0;
				while (_loc_6 < param1.length)
				{

					if (_loc_5.userid == param1[_loc_6])
					{
						_loc_5.state=param2;
						this._arrFriend.splice(_loc_4, 1);
						if (param2)
						{
							this._arrFriend.unshift(_loc_5);
						}
						else
						{
							this._arrFriend.push(_loc_5);
						}
						break;
					}
					_loc_6=_loc_6 + 1;
				}
				_loc_4=_loc_4 + 1;
			}
			this.sendNotification(CLZNoticeName.UPDATE_FRIEND_STATE, this._arrFriend);
			return;
		}

		public function requestToDayCornucopiaList(pageIndex:int):void
		{
//			var userData:UserData=new UserData();
//			userData.requestToDayCornucopiaList(pageIndex, this.responseToDayCornucopiaList);
			this._serverPoxy.writeBegin(Command.CMD_LobbyCmd);
			this._serverPoxy.writeByte(105);
			this._serverPoxy.writeInt(pageIndex);
			this._serverPoxy.writeEnd();
			this._serverPoxy.sendCmd();
			return;
		}

		public function responseToDayCornucopiaList(param1:Object):void
		{
			this.sendNotification(CLZNoticeName.CORNUCOPIA_TODAY_LIST_RES, param1.data);
			return;
		}

		public function requestMyCornucopiaList():void
		{
			this._serverPoxy.writeBegin(Command.CMD_GameRoomCmd);
			this._serverPoxy.writeByte(106);
			this._serverPoxy.writeEnd();
			this._serverPoxy.sendCmd();
			return;
		}

		public function responseMyCornucopiaList(param1:Object):void
		{
			this.sendNotification(CLZNoticeName.CORNUCOPIA_MY_LIST_RES, param1.data.arr);
			return;
		}

		public function requestCompleteNewHelp():void
		{
			var _loc_1:*=new UserData();
			_loc_1.requestCompleteNewHelp(this.responseCompleteNewHelp);
			return;
		}

		public function responseCompleteNewHelp(param1:Object):void
		{
			if (param1.svflag == 1)
			{
				this._global.userInfo.gold=param1.data.arr[0];
				this.sendNotification(CLZNoticeName.SHOW_MOVE_TIP, {tip: GameData.replaceStrValue(GameData.tip["NEW_HELP_REWARD_TIP"], "NUM", param1.data.arr[1])});
			}
			return;
		}

		public function requestGameMessageList(param1:Boolean=false):void
		{
//			var _loc_2:*=new UserData();
//			if (param1)
//			{
//				_loc_2.requestGameMessageList(this.responseInitGameMessageList);
//			}
//			else
//			{
//				_loc_2.requestGameMessageList(this.responseGameMessageList);
//			}
//			this._serverPoxy.writeBegin(Command.CMD_GameMessageList);
			this._serverPoxy.writeBegin(Command.CMD_LobbyCmd);
			this._serverPoxy.writeShort(Command.CMD_GameMessageList);
			this._serverPoxy.writeEnd();
			this._serverPoxy.sendCmd();
			return;
		}

		public function responseInitGameMessageList(param1:Object):void
		{
			var _loc_2:*=param1.data.arr;
			var _loc_3:*=this.messageHandler(_loc_2);
			this.sendNotification(CLZNoticeName.MESSAGE_SYSTEM_RES, _loc_3);
			return;
		}

		/**
		 * 接受服务端公告消息
		 */
		public function responseGameMessageList():void
		{
			var total:int=this._serverPoxy.readInt();
			var index:int=0;
			var arrGameMsg:Array=[];
			while (index < total)
			{
				index++;
				var msgVo:MessageVo=new MessageVo();
				msgVo.id=this._serverPoxy.readInt();
				msgVo.title=this._serverPoxy.readString();
				msgVo.content=this._serverPoxy.readString();
				msgVo.type=GameData.SYSTEM_MESSAGE;
				arrGameMsg.push(msgVo);
			}

//			var _loc_3:*=this.messageHandler(arrGameMsg);
			this.sendNotification(CLZNoticeName.MESSAGE_SYSTEM_RES, arrGameMsg);
			return;
		}

		private function messageHandler(param1:Array):Array
		{
			var _loc_4:MessageVo=null;
			var _loc_2:Array=[];
			var _loc_3:int=0;
			while (_loc_3 < param1.length)
			{

				_loc_4=new MessageVo();
				_loc_4.title=param1[_loc_3][0];
				_loc_4.content=param1[_loc_3][1];
				_loc_4.imgURL=param1[_loc_3][2];
				_loc_4.url=param1[_loc_3][3];
				_loc_4.type=param1[_loc_3][4];
				_loc_4.startTime=param1[_loc_3][5];
				_loc_4.endTime=param1[_loc_3][6];
				_loc_4.id=param1[_loc_3][7];
				_loc_2.push(_loc_4);
				_loc_3=_loc_3 + 1;
			}
			return _loc_2;
		}

	}
}
